/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db_vdumper.ui.component.db;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLWarning;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import org.opu.db_vdumper.util.datacopy.DbResponse;

/**
 *
 * @author yura
 */
public class DbResponseViewer extends JDialog{
    public static final int STATUS_UNDEFINED = 0;
    public static final int STATUS_CONTINUE = 1;
    public static final int STATUS_BREAK = 2;

    private Map<String, DbResponse> data;
    private DbResponseViewerI18n i18n;
    private int status = STATUS_UNDEFINED;

    public DbResponseViewer(JFrame m, Map<String, DbResponse> data,
            DbResponseViewerI18n i18n) {

        super(m, m.getTitle(), true);
        this.data = data;
        this.i18n = i18n;

        setTitle(i18n.getTitle());
        initComponent();
    }

    public DbResponseViewer(JFrame m, Map<String, DbResponse> data){
        this(m, data, new DefaultDbResponseViewerI18n());
    }

    private void initComponent() {
        String o1 = i18n.getRoot();
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(o1);
        JTree tree = new JTree(new DefaultTreeModel(root));
        Object o2 = null;

        for (String name : data.keySet()) {
            DbResponse get = data.get(name);
            String s = get.isSuccess() ? i18n.getSuccess() : i18n.getFailure();

            String val = s + " - " + name;
            DefaultMutableTreeNode elem = new DefaultMutableTreeNode(val);
            root.add(elem);

            if (o2 == null){
                o2 = val;
            }

            SQLWarning w = get.getWarning();
            if (w != null){
                elem.add(new DefaultMutableTreeNode(w.toString()));
                while((w = w.getNextWarning()) != null){
                    elem.add(new DefaultMutableTreeNode(w.toString()));
                }
            }
        }

        if (o2 == null) {
            tree.makeVisible(new TreePath(root));
        } else {
            tree.makeVisible(new TreePath(new Object[]{root, o2}));
        }

        JButton contin = new JButton(new AbstractAction(i18n.getContinue()) {

            @Override
            public void actionPerformed(ActionEvent e) {
                int old = status;
                status = STATUS_CONTINUE;
                firePropertyChange("maction", old, status);
                setVisible(false);
            }
        });

        JButton brk = new JButton(new AbstractAction(i18n.getBreak()) {

            @Override
            public void actionPerformed(ActionEvent e) {
                int old = status;
                status = STATUS_BREAK;
                firePropertyChange("maction", old, status);
                setVisible(false);
            }
        });

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                int old = status;
                status = STATUS_BREAK;
                firePropertyChange("maction", old, status);
            }

        });

        Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalGlue());
        box.add(contin);
        box.add(Box.createHorizontalStrut(10));
        box.add(brk);
        box.add(Box.createHorizontalGlue());

        Container contentPane = getContentPane();
        contentPane.add(new JScrollPane(tree));
        contentPane.add(box, BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(null);
    }

    public static int showDialog(JFrame m, Map<String, DbResponse> data){
        DbResponseViewer v = new DbResponseViewer(m, data);
        v.setVisible(true);
        return v.status;
    }

}
